package com.crm.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.base.ResponseResult;
import com.crm.domain.dto.ClueDto;
import com.crm.domain.entity.Activity;
import com.crm.domain.entity.Assignment;
import com.crm.domain.entity.Clue;
import com.crm.domain.query.ClueQuery;
import com.crm.service.ActivityService;
import com.crm.service.IClueService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequiredArgsConstructor
@RequestMapping("/clues")
public class ClueController {

    private final IClueService clueService;

    private final ActivityService activityService;

    /**
     * 分页条件查询
     * @param query
     * @return
     */
    @GetMapping("clue/list")
    public ResponseResult clueList(ClueQuery query){
        Page<Clue> queryPage = clueService.queryPage(query);
        return ResponseResult.success()
                .put("total", queryPage.getTotal())
                .put("rows", queryPage.getRecords())
                .put("parms",null);
    }
    /**
     * 添加线索
     * @param clueDto
     * @return
     */
    @PostMapping("clue")
    public ResponseResult addClue(ClueDto clueDto){
        Clue clue = BeanUtil.copyProperties(clueDto, Clue.class);
        clueService.save(clue);
        return ResponseResult.success();
    }

    @GetMapping("/clue/{id}")
    public ResponseResult getById(@PathVariable Long id){
        Clue clue = clueService.getById(id);
        return ResponseResult.success(clue);
    }
    @GetMapping("/record/list")
    public ResponseResult getRecordById(@RequestParam("clueId") Long id){
        return ResponseResult.success(clueService.getById(id));
    }

    @GetMapping("/activity/listselect/{id}")
    public ResponseResult getActivityById(@PathVariable Long id){
        Activity serviceById = activityService.getById(id);
        return ResponseResult.success(serviceById);

    }
    @PutMapping("/clue/assignment")
    public ResponseResult updateClue(@RequestBody Assignment assignment){
        Wrapper<Assignment> wrapper = Wrappers.<Assignment>lambdaUpdate();
//        clueService.update(wrapper);
        return ResponseResult.success();
    }
    @PostMapping("/clue/importData")
    public ResponseResult batchImportData(@RequestBody MultipartFile file) throws IOException {
        clueService.importData(file);
        return ResponseResult.success();
    }
}
